package com.oauth.serviceImpl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oauth.dao.UserDao;
import com.oauth.entity.LoginUser;
import com.oauth.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ysy
 */
@Service("userDetailsService")
public class MyUserDetailServiceImpl extends ServiceImpl<UserDao, User> implements UserDetailsService {
    @Autowired
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();
        user.setUserName(username);
        User user1 = userDao.selectOne(user);
        if (Objects.isNull(user1)) {
            throw new RuntimeException("用户名或者密码错误！");
        }
        // 根据用户Id去查询我们的数据，权限验证
        List<String> permissionList = new ArrayList<>();
                // userDao.selectPermission(user1.getId());
        permissionList.add("hello");
        //封装成UserDetails对象返回
        return new LoginUser(user1,permissionList);
    }
}
